﻿$(document).ready(function () {

    getEconomicGroupList();
    getSectorList();
    
    $('#add-businnes').click(function () {
        jsonEconomicGroup = {
            name: $('#txtEconomicGroupName').val(),
            credibility: $('#optEconomicGroupReputation').val(),
        };

        insertBusinessGroup(jsonEconomicGroup);
        getEconomicGroupList();
    });

    $('#add-enterprise').click(function () {
        jsonEnterprise = {
            ruc: $('#txtRUC').val(),
            socialReason: $('#txtSocialReason').val(),
            sectorId: $('#optSector').val(),
            cem: $('#txtRisk').val(),
            creationDate: $('#txtDate').val(),
            groupId: $('#optEconomicGroup').val(),
            defaultState: $('#txtValue').val(),
            agentName: $('#txtName').val(),
            agentFirstName: $('#txtFirstName').val(),
            agentLastName: $('#txtLastName').val(),
            officialAddress: $('#txtAddress').val(),
            agentDNI: $('#txtDNI').val(),
            agentPhone: $('#txtPhone').val(),
            agentEmail: $('#txtEmail').val(),
        };

        insertEnterprise(jsonEnterprise);
    });

});

function getEconomicGroupList() {

    $.ajax({
        type: "POST",
        data: JSON.stringify(),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "getEconomicGroupList",

        success: function (data) {

            var result = "";

            $.each(data, function (i, item) {

                result += '<option value="' + item.economicGroupId + '">' + item.name + '</option>';

            });

            $('#optEconomicGroup').html(result);


        }

    });//Fin del Ajax
}

function getSectorList() {

    $.ajax({
        type: "POST",
        data: JSON.stringify(),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "getSectorList",

        success: function (data) {

            var result = "";

            $.each(data, function (i, item) {

                result += '<option value="' + item.sectorId + '">' + item.name + '</option>';

            });

            $('#optSector').html(result);



        }

    });//Fin del Ajax

}



function insertEnterprise(jsonEnterprise) {
    $.ajax({
        type: "POST",
        data: JSON.stringify(jsonEnterprise),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "insertEnterprise",
        success: function (data) {

        }

    });//Fin del Ajax
}


function insertBusinessGroup(jsonBusinessGroup) {
    $.ajax({
        type: "POST",
        data: JSON.stringify(jsonBusinessGroup),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: "insertBusinessGroup",
        success: function (data) {

        }

    });//Fin del Ajax
}
